<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use think\Db;

/**
 * FB账户管理
 *
 * @icon fa fa-circle-o
 */
class Feditionaccountfb extends Backend
{
    
    /**
     * Feditionaccountfb模型对象
     * @var \app\admin\model\Feditionaccountfb
     */
    protected $model = null;
    protected $dataLimit = 'auth';
    protected $dataLimitField = 'admin_id';
    
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Feditionaccountfb;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 导入
     */
    public function import()
    {
        $file = $this->request->request('file');
        if (!$file) {
            $this->error(__('Parameter %s can not be empty', 'file'));
        }
        $filePath = ROOT_PATH . DS . 'public' . DS . $file;
        if (!is_file($filePath)) {
            $this->error(__('No results were found'));
        }
        //实例化reader
        $ext = pathinfo($filePath, PATHINFO_EXTENSION);

        if ($ext == 'txt') {
            header("content-type:text/html;charset=utf-8");
            $file = $filePath;
            if(file_exists($file)){
                $file_arr = file($file); ###得到数组
                $arr_new = array();
                foreach($file_arr as $v){
                    $a = trim($v);
                    $a = str_replace("\r\n","",$a);
                    $a = str_replace("\r","",$a);
                    $a = str_replace("\n","",$a);
                    if (!empty($a)) {
                        $arr_new[] = $a;
                    }
                }
                //开始导入
                $addnum = 0;
                $editnum = 0;
                foreach ($arr_new as $item) {
                    $newitems = array();
                    $newitem = explode('|',$item);
                    $newitem = array_filter($newitem);
                    foreach ($newitem as $vi) {
                        if (!empty($vi)) {
                            $newitems[] = $vi;
                        }
                    }
                    if (!empty($newitems)) {
                        $additem = array();
                        $additem['username'] = $newitems[0];
                        $additem['admin_id'] = $this->auth->id;
                        $additem['password'] = $newitems[1];
                        $additem['code'] = $newitems[2];
                        $additem['secretkey'] = $newitems[3];
                        $additem['email'] = $newitems[4];
                        $additem['secretvalue'] = $newitems[5];
                        $accountfb = Db::name('fedition_accountfb')->where('username',$additem['username'])->find();
                        if (!empty($accountfb)) {
                            $additem['updatetime'] = time();
                            Db::name('fedition_accountfb')->where('username',$additem['username'])->update($additem);
                            $editnum += 1;
                        } else {
                            $additem['createtime'] = time();
                            Db::name('fedition_accountfb')->insert($additem);
                            $addnum += 1;
                        }
                    }
                }
                $this->success('成功添加'.$addnum.'条，修改'.$editnum.'条');
            }else{
                $this->error(__('file not exists!'));
            }
        }
        $this->success();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $lists = $list->items();
            if($lists){
                foreach ($lists as $k=>$v){
                    $admin = Db::name('admin')->where('id', '=', $v['admin_id'])->find();
                    if($admin){
                        $lists[$k]['admin_name'] = $admin['nickname'];
                    }else{
                        $lists[$k]['admin_name'] = '管理员未设置';
                    }
                }
            }
            $result = array("total" => $list->total(), "rows" => $lists);
            return json($result);
        }
        return $this->view->fetch();
    }
}
